package cn.wowjoy.pda.android_pharmacy.ui.activity.hospital.take;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;

import cn.wowjoy.pda.android_pharmacy.R;
import cn.wowjoy.pda.android_pharmacy.common.core.BaseActivity;
import cn.wowjoy.pda.android_pharmacy.common.utils.Contants;
import cn.wowjoy.pda.android_pharmacy.common.widget.CreateDialog;
import cn.wowjoy.pda.android_pharmacy.common.widget.MDialog;
import cn.wowjoy.pda.android_pharmacy.common.widget.MyToast;
import cn.wowjoy.pda.android_pharmacy.databinding.ActivityInHospitalTakeProgressDetailBinding;
import cn.wowjoy.pda.android_pharmacy.model.xmlEntity.inHospitalPatientInfo.InHospitalPatientBeen;
import cn.wowjoy.pda.android_pharmacy.mvvm.presenter.InHospitalTakeProgressDetailPresenter;
import cn.wowjoy.pda.android_pharmacy.mvvm.vm.InHospitalTakeProgressDetailViewModel;

public class InHospitalTakeProgressDetailActivity extends BaseActivity<ActivityInHospitalTakeProgressDetailBinding,InHospitalTakeProgressDetailViewModel,InHospitalTakeProgressDetailPresenter> {

    private MDialog mDialog;

    public static final int CLOSE=1;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case CLOSE:
                    //TODO:3秒后自动跳转
                    if(null != mDialog){
                        mDialog.dismiss();
                        setResult(Contants.RESULT_SUCCESS_CODE);
                        finish();
                    }
                    break;
            }
        }
    };


    @Override
    protected void init(Bundle savedInstanceState) {
        binding.setViewModel(viewModel);
        binding.setPresenter(presenter);
        binding.doorScanResultTitle.titleTextTv.setText("扫码取药");
        //TODO:界面信息的更细
        final InHospitalPatientBeen topRemove = (InHospitalPatientBeen) getIntent().getExtras().getSerializable("topRemove");
        if(null != topRemove){
            updateUI(topRemove);
        }


        //TODO:应取数量和取药数量的对比
        binding.btSureDoorScanResult.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (binding.numFactDoorScanResult.getText().length() == 0) {
                    new MyToast(InHospitalTakeProgressDetailActivity.this).showinfo("请输入取药数量");
                } else {
                    if (presenter.check(binding.numShouldDoorScanResult.getText().toString().trim(), binding.numFactDoorScanResult.getText().toString().trim())) {
                        //TODO:弹框 药品Name

                        mDialog = CreateDialog.successDialog(InHospitalTakeProgressDetailActivity.this,6 ,"取药成功");
                        mHandler.sendEmptyMessageDelayed(CLOSE,800);
                    }else{
                        binding.numFactDoorScanResult.setText("");
                    }
                }
            }
        });
    }

    @Override
    public void onBackPressed() {
        setResult(Contants.RESULT_FAIL_CODE);
        super.onBackPressed();
    }

    private void updateUI(InHospitalPatientBeen topRemove) {
        if(null != topRemove.getSFGWPB() && !"".equals(topRemove.getSFGWPB())){
            if(Integer.parseInt(topRemove.getSFGWPB()) == 1){
                binding.medicineNameDoorScanResult.setTextColor(getResources().getColor(R.color.red));
            }else{
                binding.medicineNameDoorScanResult.setTextColor(getResources().getColor(R.color.text_undanger));
            }
        }
        binding.medicineNameDoorScanResult.setText(topRemove.getZZYPMC()+"  "+topRemove.getZZYPGG());
        binding.numShouldDoorScanResult.setText(topRemove.getCFYPSL());
                //取消输入数量
        binding.numFactDoorScanResult.setText(topRemove.getCFYPSL());
        binding.numFactDoorScanResult.setSelection(binding.numFactDoorScanResult.getText().length());

        binding.placeDoorScanResult.setText(topRemove.getYPCDMC());
        binding.zhiDoorScanResult.setText(" "+topRemove.getYPBZDW());
        binding.zhi2DoorScanResult.setText(" "+topRemove.getYPBZDW());
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_in_hospital_take_progress_detail;
    }

    @Override
    protected Class<InHospitalTakeProgressDetailPresenter> getPresenterClass() {
        return InHospitalTakeProgressDetailPresenter.class;
    }

    @Override
    protected Class<InHospitalTakeProgressDetailViewModel> getViewModelClass() {
        return InHospitalTakeProgressDetailViewModel.class;
    }
}
